package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.vo.*;
import com.gxa.entity.SysDiagnosis;
import com.gxa.mapper.DrugManagementMapper;
import com.gxa.mapper.SysDiagnosisMapper;
import com.gxa.service.SysDiagnosisService;
import com.gxa.service.SysDrugTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service
public class SysDiagnosisServiceImpl implements SysDiagnosisService {
    @Autowired
    SysDiagnosisMapper sysDiagnosisMapper;
    @Autowired
    DrugManagementMapper drugManagementMapper;

    @Autowired
    SysDrugTotalService service;

    /**
     * 添加诊断信息
     * @param sysDiagnosis 诊断实体
     * @return R
     */
    @Override
    public R addSysDiagnosis(SysDiagnosis sysDiagnosis) {

        R r = new R();

        try {
            sysDiagnosisMapper.addSysDiagnosis(sysDiagnosis);
            r.setCode(200);
            r.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;

    }


    /**
     * 分页查询所有诊断列表
     * @param currentPage 当前页
     * @param pageSize 页面大小
     * @return R
     */
    @Override
    public R selectAllDiagnosis(Integer currentPage, Integer pageSize) {
        R r=new R();
        System.out.println();
        System.out.println(currentPage+pageSize);
        try {
            PageHelper.startPage(currentPage,pageSize);
            List<FindDiagnosis> diagnosisList = sysDiagnosisMapper.selectAllDiagnosis();
            PageInfo<FindDiagnosis> pageInfo = new PageInfo(diagnosisList);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }



    /**
     * 根据状态分页查询所有诊断信息
     * @param currentPage 当前页
     * @param pageSize 页面大小
     * @return R
     */
    @Override
    public R selectDiagnosisByStatus(Integer status, Integer currentPage, Integer pageSize) {
        R r=new R();

        try {
            PageHelper.startPage(currentPage,pageSize);
            List<FindDiagnosis> diagnosisList = sysDiagnosisMapper.selectDiagnosisByStatus(status);
            PageInfo<FindDiagnosis> pageInfo = new PageInfo(diagnosisList);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    /**
     * 根据患者名字查询所有诊断信息
     * @param name 患者名
     * @param currentPage 当前页
     * @param pageSize 页面大小
     * @return R
     */
    @Override
    public R findDiagnosisByPatientCondition(String name, Integer currentPage, Integer pageSize) {
        R r=new R();

        try {
            PageHelper.startPage(currentPage,pageSize);
            List<FindDiagnosis> diagnosisList = sysDiagnosisMapper.findDiagnosisByPatientCondition(name);
            PageInfo<FindDiagnosis> pageInfo = new PageInfo(diagnosisList);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    /**
     * 根据诊断编号删除诊断信息
     * @param id 诊断编号
     * @return R
     */
    @Override
    public R selectDiagnosisById(Integer id) {
        R r=new R();
        try {
            FindDiagnosis diagnosis= sysDiagnosisMapper.selectDiagnosisById(id);
            r.setCode(200);
            r.setMsg("success");
            r.setData(diagnosis);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    /**
     * 根据诊断编号删除诊断信息
     * @param id 诊断编号
     * @return R
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,timeout=5,rollbackFor = {Exception.class})
    public R delDiagnosisById(Integer id) {
        R r=new R();
        try {
            sysDiagnosisMapper.delDiagnosisById(id);
            sysDiagnosisMapper.delDiagnosisDrugById(id);
            r.setCode(200);
            r.setMsg("success");
            return r;
        }catch (Exception e){
            throw new RuntimeException("数据库发生了异常！！！");
        }

    }

    /**
     * 修改诊断信息
     * @param sysDiagnosis 诊断实体
     * @return R
     */
    @Override
    public R updateDiagnosisById(SysDiagnosis sysDiagnosis) {
        R r = new R();
        try{
            sysDiagnosisMapper.updateDiagnosisById(sysDiagnosis);
            r.setCode(200);
            r.setMsg("success");
        }catch(Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 根据诊断编号分页查询诊断药品
     * @param diagnosisId 诊断编号
     * @param currentPage 当前页
     * @param pageSize 页面大小
     * @return R
     */
    @Override
    public R selectDiagnosisDrugById(Integer diagnosisId,Integer currentPage, Integer pageSize) {
        R r=new R();

        try {
            PageHelper.startPage(currentPage,pageSize);
            List<DiagnosisDrugVo> diagnosisDrugVos = sysDiagnosisMapper.selectDiagnosisDrugByDiagnosisId(diagnosisId);
            PageInfo<FindDiagnosis> pageInfo = new PageInfo(diagnosisDrugVos);
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 根据诊断编号药品编号回显诊断药品信息
     * @param drugId 药品ID
     * @param id 诊断药品主ID
     * @return R
     */
    @Override
    public R findDiagnosisDrugByDrug(Integer drugId, Integer id) {
        R r=new R();

        try {
            DiagnosisDrugVo diagnosisDrugVo = sysDiagnosisMapper.selectDiagnosisDrugByDrug(drugId, id);
            r.setCode(200);
            r.setMsg("success");
            r.setData(diagnosisDrugVo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 修改药品数量
     * @param num 药品数量
     * @param id 诊断药品主ID
     * @param drugId 药品数量
     * @return R
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,timeout=5,rollbackFor = {Exception.class})
    public R updateDiagnosisDrug(Integer num,Integer id,Integer drugId) {
        R r=new R();
        if(num!=null&&id!=null&&drugId!=null){
            try {
                Integer num1 = drugManagementMapper.selectNumById(drugId);
                if (num<=num1){
                    Integer diagId = sysDiagnosisMapper.selectDiagByMainId(id);
                    FindDiagnosis findDiagnosis = sysDiagnosisMapper.selectDiagnosisById(diagId);
                    Integer userId = findDiagnosis.getUserId();
                    service.drugRecord(0,drugId,num,"诊断开单"+findDiagnosis.getClinicNumber(),userId);
                    sysDiagnosisMapper.updateDiagnosisDrug(num,id);
                    r.setCode(200);
                    r.setMsg("success");
                }else {
                    r.setCode(400);
                    r.setMsg("药品数量不够啦，已经超出药品存库数量");
                }
            }catch (Exception e){
                throw new RuntimeException("数据库发生了异常！！！");
            }
        }else{
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 根据药品编号，诊断编号删除诊断药品信息
     * @param id 诊断ID
     * @return R
     */
    @Override
    public R delDiagnosisDrug(Integer id) {
        R r=new R();
        if(id!=null){
            try {
                sysDiagnosisMapper.delDiagnosisDrug(id);
                r.setCode(200);
                r.setMsg("success");
            }catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
        }else{
            r.setCode(400);
            r.setMsg("参数不能为空");
        }
        return r;
    }

    /**
     * 在诊断单里面添加药品
     * @param diagnosisDrugVo 诊断药品Vo
     * @return R
     */
    @Override
    @Transactional
    public R addDiagnosisDrug(DiagnosisDrugVo diagnosisDrugVo) {
        R r=new R();
        Integer drugId = diagnosisDrugVo.getDrugId();
        Integer num = diagnosisDrugVo.getNum();
        Integer num1 = drugManagementMapper.selectNumById(drugId);
        if (drugId !=null&&num<=num1){
            try {
                FindDiagnosis findDiagnosis = sysDiagnosisMapper.selectDiagnosisById(diagnosisDrugVo.getDiagnosisId());
                Integer userId = findDiagnosis.getUserId();
                System.out.println(findDiagnosis);
                service.drugRecord(0,drugId,num,"诊断开单"+findDiagnosis.getClinicNumber(),userId);
                sysDiagnosisMapper.addDiagnosisDrug(diagnosisDrugVo);
                r.setCode(200);
                r.setMsg("success");
            }catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("药品数量不够啦，已经超出药品存库数量");
        }
        return r;
    }


    /**
     *
     * @return
     */
    @Override
    public R countDiagnosis() {
        R r=new R();

        try {
            CountDiagnosisVo countDiagnosisVo = sysDiagnosisMapper.countDiagnosis();
            r.setCode(200);
            r.setMsg("success");
            r.setData(countDiagnosisVo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    @Override
    public R countDiagnosisByType() {
        R r=new R();

        try {
            List<CountDiagnosisVo> countDiagnosisVos = sysDiagnosisMapper.countDiagnosisByType();
            r.setCode(200);
            r.setMsg("success");
            r.setData(countDiagnosisVos);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R countDiagnosisDrugs(Byte type) {
        R r=new R();

        try {
            List<CountDiagnosisVo> countDiagnosisVos = sysDiagnosisMapper.countDiagnosisDrugs(type);
            r.setCode(200);
            r.setMsg("success");
            r.setData(countDiagnosisVos);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }


    @Override
    public R countDiagnosisMonth(Byte type) {
        R r=new R();

        try {
            List<CountDiagnosisVo> countDiagnosisVos = sysDiagnosisMapper.countDiagnosisMonth(type);
            r.setCode(200);
            r.setMsg("success");
            r.setData(countDiagnosisVos);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R countDiagnosisMonthDay(Byte type) {
        R r=new R();

        try {
            List<CountDiagnosisVo> countDiagnosisVos = sysDiagnosisMapper.countDiagnosisMonthDay(type);
            r.setCode(200);
            r.setMsg("success");
            r.setData(countDiagnosisVos);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 统计所有诊断数量
     * @return R
     */
    @Override
    public R countAllDiagnosis() {
        R r=new R();

        try {
            Integer count = sysDiagnosisMapper.countAllDiagnosis();
            r.setCode(200);
            r.setMsg("success");
            r.setData(count);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    /**
     * 统计周所有诊断数量
     * @return R
     */
    @Override
    public R countWeekDiagnosis() {
        R r=new R();

        try {
            List<CountDiagnosisVo> countDiagnosisVo = sysDiagnosisMapper.countWeekDiagnosis();
            r.setCode(200);
            r.setMsg("success");
            r.setData(countDiagnosisVo);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }




    /**
     * 根据条件模糊查询
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @param condition 条件
     * @return R
     */
    @Override
    public R findDiagDrugByCondition(Integer currentPage, Integer pageSize, String condition,Integer diagnosisId) {
        R r=new R();
        if (condition!=null&&currentPage!=null&&pageSize!=null){
            try {
                PageHelper.startPage(currentPage,pageSize);
                List<DiagnosisDrugVo> diagnosisDrugVoList = sysDiagnosisMapper.findDiagDrugByCondition(condition,diagnosisId);
                PageInfo<FindDiagnosis> pageInfo = new PageInfo(diagnosisDrugVoList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(pageInfo);
            }catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
        }else {
            r.setCode(400);
            r.setMsg("参数请补全");
        }
        return r;
    }
}
